home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-installer.exe / bin / quodlibet / util / uri.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  4KB  |  116 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. import os
  5. import re
  6. from urllib import url2pathname, quote_plus, unquote_plus
  7. from urlparse import urlparse, urlunparse
  8. from quodlibet.util.path import fsdecode, pathname2url
  9.  
  10. class URI(str):
  11.     '''A full URI string. This object provides several convenience
  12.     attributes to access data from the urlparse and urllib modules.
  13.  
  14.     URIs inherit from str, and so any method that works on a str
  15.     works on a URI.
  16.  
  17.     URIs are not closed under concatenation, slicing, and so on. Neither
  18.     are these URI objects; such operations will return strs.'''
  19.     
  20.     def __new__(klass, value, escaped = True):
  21.         '''Create a new URI object. By default, the URI is assumed to be
  22.         escaped already. Pass escaped=False if you need the URI escaped
  23.         (this is imperfect now).
  24.  
  25.         The URI returned will be equivalent, but not necessarily
  26.         equal, to the value passed in.'''
  27.         value = re.sub('^([A-Za-z]+):///+', '\\1:///', value)
  28.         values = list(urlparse(value))
  29.         if not escaped:
  30.             values[2] = quote_plus(values[2], safe = '/~')
  31.         value = urlunparse(values)
  32.         obj = str.__new__(klass, value)
  33.         if not (obj.scheme) or not (len(obj.scheme) > 1):
  34.             raise ValueError("URIs must have a scheme, such as 'http://'")
  35.         if not obj.netloc or obj.path:
  36.             raise ValueError('URIs must have a network location or path')
  37.         return obj
  38.  
  39.     
  40.     def frompath(klass, value):
  41.         '''Construct a URI from an unescaped filename.'''
  42.         return klass('file://' + pathname2url(value), escaped = True)
  43.  
  44.     frompath = classmethod(frompath)
  45.     
  46.     def scheme(self):
  47.         """URI scheme (e.g. 'http')"""
  48.         return urlparse(self)[0]
  49.  
  50.     scheme = property(scheme)
  51.     
  52.     def netloc(self):
  53.         """URI network location (e.g. 'example.com:21')"""
  54.         return urlparse(self)[1]
  55.  
  56.     netloc = property(netloc)
  57.     
  58.     def path(self):
  59.         """URI path (e.g. '/~user')"""
  60.         return urlparse(self)[2]
  61.  
  62.     path = property(path)
  63.     
  64.     def params(self):
  65.         '''URI parameters'''
  66.         return urlparse(self)[3]
  67.  
  68.     params = property(params)
  69.     
  70.     def query(self):
  71.         """URI query string (e.g. 'foo=bar&a=b')"""
  72.         return urlparse(self)[4]
  73.  
  74.     query = property(query)
  75.     
  76.     def fragment(self):
  77.         """URI fragment ('foo' in '#foo')"""
  78.         return urlparse(self)[5]
  79.  
  80.     fragment = property(fragment)
  81.     
  82.     def unescaped(self):
  83.         '''an unescaped str (not URI) version of the URI'''
  84.         values = list(urlparse(self))
  85.         values[2] = unquote_plus(values[2])
  86.         return urlunparse(values)
  87.  
  88.     unescaped = property(unescaped)
  89.     
  90.     def filename(self):
  91.         '''a local filename equivalent to the URI'''
  92.         if self.scheme != 'file':
  93.             raise ValueError('only the file scheme supports filenames')
  94.         if self.netloc:
  95.             raise ValueError('only local files have filenames')
  96.         if os.name == 'nt':
  97.             return fsdecode(url2pathname(self.path))
  98.         return None(self.path)
  99.  
  100.     filename = property(filename)
  101.     
  102.     def is_filename(self):
  103.         '''True if the URI is a valid (not necessarily existing)
  104.         local filename
  105.         '''
  106.         if self.scheme == 'file':
  107.             pass
  108.         return not (self.netloc)
  109.  
  110.     is_filename = property(is_filename)
  111.     
  112.     def __repr__(self):
  113.         return '<%s %r>' % (type(self).__name__, self.unescaped)
  114.  
  115.  
  116.